package com.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 386. 字典序排数
 * DFS
 *
 * @author fy
 * @date 2022/4/18 10:39
 */
public class Solution386_1 {

    public List<Integer> lexicalOrder(int n) {
        List<Integer> res = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            dfs(i, res, n);
        }
        return res;
    }

    private void dfs(int num, List<Integer> res, int n) {
        if (num > n) {
            return;
        }
        res.add(num);
        for (int i = 0; i <= 9; i++) {
            dfs(num * 10 + i, res, n);
        }
    }

    public static void main(String[] args) {
        List<Integer> res = new Solution386_1().lexicalOrder(9999);
        System.out.println(res);
    }

}
